//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.tech.zhidengxian.util;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

public class JacksonUtils {
    private static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    private static final ObjectMapper ABJECT_MAPPER;

    public JacksonUtils() {
    }

    public static String serialize(Object object) {
        if (object == null) {
            return null;
        } else {
            try {
                return ABJECT_MAPPER.writeValueAsString(object);
            } catch (Exception var2) {
                throw new RuntimeException("Failed to write object as json: " + object, var2);
            }
        }
    }

    public static <T> T deserialize(String string, TypeReference<T> typeReference) {
        if (StringUtils.isBlank(string)) {
            return null;
        } else {
            try {
                return ABJECT_MAPPER.readValue(string, typeReference);
            } catch (Exception var3) {
                throw new RuntimeException("Failed to read json as object, type: " + typeReference + ", content: " + string, var3);
            }
        }
    }

    public static <T1, T2> T2 convert(T1 source, TypeReference<T2> type) {
        return source == null ? null : deserialize(serialize(source), type);
    }

    public static <T1, T2> T2 convert(T1 source, Class<T2> type) {
        return source == null ? null : deserialize(serialize(source), type);
    }

    public static <T1, T2> List<T2> convertList(List<T1> source) {
        return source == null ? null : (List)deserialize(serialize(source), new TypeReference<List<T2>>() {
        });
    }

    public static <T> T deserialize(String string, Class<T> type) {
        if (StringUtils.isBlank(string)) {
            return null;
        } else {
            try {
                return ABJECT_MAPPER.readValue(string, type);
            } catch (Exception var3) {
                throw new RuntimeException("Failed to read json as object, type: " + type + ", content: " + string, var3);
            }
        }
    }

    public static <T> List<T> deserializeToList(String source, Class<T> type) {
        if (StringUtils.isBlank(source)) {
            return null;
        } else {
            try {
                JavaType javaType = ABJECT_MAPPER.getTypeFactory().constructParametrizedType(ArrayList.class, List.class, new Class[]{type});
                return (List)ABJECT_MAPPER.readValue(source, javaType);
            } catch (IOException var3) {
                throw new RuntimeException("Failed to read json as List<javaBean>, type: " + type + ", content: " + source, var3);
            }
        }
    }

    public static <S, T> List<T> convertList(List<S> sources, Class<T> clazz) {
        if (CollectionUtils.isEmpty(sources)) {
            return Lists.newArrayList();
        } else {
            ArrayList<T> ts = new ArrayList();
            sources.forEach((en) -> {
                ts.add(convert(en, clazz));
            });
            return ts;
        }
    }

    static {
        ABJECT_MAPPER = (new ObjectMapper()).setSerializationInclusion(Include.NON_NULL).disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES).setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
    }
}
